package com.nginxmanager.controller;

import com.nginxmanager.domain.Conf;
import com.nginxmanager.server.serverIpm.FileServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-02-01.
 */
@RestController
@RequestMapping("/nginx")
@Api(tags = "Nginx日志文件")
public class LogController {

    FileServer fileServer = new FileServer();

    @GetMapping("/errorConf")
    @ApiOperation("查看错误日志文件")
    public ResponseEntity<List<String>> errorLog(@RequestParam("address")String address ){
        List<String> list = new ArrayList<String>();
        String path = address+"\\logs\\error.log";
        try {
            list= fileServer.readLog(path);
        }catch (Exception e){
            return ResponseEntity.status(412).body(null);
        }
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }

    @GetMapping("/contentLog")
    @ApiOperation("查看连接日志文件文件")
    public ResponseEntity<List<String>> contentLog(@RequestParam("address")String address ){
        List<String> list = new ArrayList<String>();
        String path = address+"\\logs\\access.log";
        try {
            list= fileServer.readLog(path);
        }catch (Exception e){
            return ResponseEntity.status(412).body(null);
        }
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }
    @GetMapping("/copyErrorFile")
    @ApiOperation("备份清楚错误日志文件")
    public ResponseEntity<Void> copyErrorFile (@RequestParam("address")String address){
        List<String> list = new ArrayList<String>();
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        String time=format.format(date);
        String path = address+"\\logs\\error.log";
        String newPath = address+"\\logs\\error"+time+".log";
        try {
            list = fileServer.copy(path,newPath);
        }catch (Exception e){
            return (ResponseEntity<Void>) ResponseEntity.badRequest();
        }
        return ResponseEntity.status(200).body(null);
    }

    @GetMapping("/copyContentFile")
    @ApiOperation("备份清空连接情况日志文件")
    public ResponseEntity<Void> copyContentFile (@RequestParam("address")String address){
        List<String> list = new ArrayList<String>();
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        String time=format.format(date);
        String path = address+"\\logs\\access.log";
        String newPath = address+"\\logs\\access"+time+".log";
        try {
            list = fileServer.copy(path,newPath);
        }catch (Exception e){
            return (ResponseEntity<Void>) ResponseEntity.badRequest();
        }
        return ResponseEntity.status(200).body(null);
    }
}
